<?php

include("connect.php");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("content-type:application/xml;charset=utf-8;");
function loguser($reason,$ext = "db")
{
    if(!is_dir("logs")) {
        mkdir("logs","0493");
    }
    $fp = fopen("logs/".date("m-d-y").".".$ext, "a+");
    fwrite($fp, "<strong>".date("g:i:s A")."</strong>: ".$reason."<br/>");
}
loguser($_SERVER['QUERY_STRING'], 'html');

$query = "select 	Person.ID, 
	Person.Surname, 
	Person.FirstNames, 
	Person.FormerName, 
	Person.NeeName, 
	Person.AlsoKnownAs, 
	Person.DeathDate, 
	Person.BirthDate, 
	Person.Age AS 'Age', 
	Person.AgeMeasure AS 'AgeMeasure', 
	CityTown.Name as 'CityTown', 
	c2.Name as 'FormerCityTown', 
	Region.Name AS 'Region', 
	Country.Name AS 'Country', 
	Charity.Name AS 'Charity', 
	FuneralHome.Name AS 'FuneralHome' 

from Person 
left join Region on Person.RegionID = Region.ID 
left join Country on Country.ID = Person.CountryID 
left join CityTown AS c2 on Person.FormerCityTownID = c2.ID 
left join Charity on Charity.ID = Person.CharityID 
left join FuneralHome on FuneralHome.ID = Person.FuneralHomeID 
left join CityTown on CityTown.ID = Person.CityTownID LIMIT 0,100";

// Perform Query
$result = mysql_query($query, $link);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><items>\n";
while ($row = mysql_fetch_assoc($result)) {
	echo "<item><value>".$row['ID']."</value>\n";
	echo "<surname>".str_replace("&", "&amp;", $row['Surname'])."</surname>";
echo "<firstnames>".str_replace("&", "&amp;", $row['FirstNames'])."</firstnames>";
echo "<formername>".str_replace("&", "&amp;", $row['FormerName'])."</formername>";
echo "<neename>".str_replace("&", "&amp;", $row['NeeName'])."</neename>";
echo "<alsoknownas>".str_replace("&", "&amp;", $row['AlsoKnownAs'])."</alsoknownas>";
echo "<deathdate>".str_replace("&", "&amp;", $row['DeathDate'])."</deathdate>";
echo "<birthdate>".str_replace("&", "&amp;", $row['BirthDate'])."</birthdate>";
echo "<age>".str_replace("&", "&amp;", $row['Age'])."</age>";
echo "<agemeasure>".str_replace("&", "&amp;", $row['AgeMeasure'])."</agemeasure>";
echo "<citytown>".str_replace("&", "&amp;", $row['CityTown'])."</citytown>";
echo "<formercitytown>".str_replace("&", "&amp;", $row['FormerCityTown'])."</formercitytown>";
echo "<region>".str_replace("&", "&amp;", $row['Region'])."</region>";
echo "<country>".str_replace("&", "&amp;", $row['Country'])."</country>";
echo "<charity>".str_replace("&", "&amp;", $row['Charity'])."</charity>";
echo "<funeralhome>".str_replace("&", "&amp;", $row['FuneralHome'])."</funeralhome>\n</item>\n";

}
echo "</items>";


// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
mysql_close($link);
?>
